// <?php
namespace  Partner\Controller;
use Utuo\BackgroundController;
class CustomerPartnerController extends BackgroundController
{     protected $cid;
	public function _initialize(){
		$this->setControllerTitle('管理员管理');
		$this->setActionTitle(array(
				'index' => '管理员列表',
				'add' => '添加管理员',
				'modify' => '修改管理员',
				'recycle' => '回收站',
	
		),'','cid='.I('cid'));
		$this->setActionNav(array(
				'index',
				'add',
				'recycle'
		));
	$this->cid=I('cid');
	}
	public function getLists($is_deleted){
	
		$condations=array(
				'status'=>1,
				'is_deleted'=>$is_deleted,
				'fid'=>$this->cid
		);
		$customerAdmin=M('Customer')->where($condations)->select();
		$this->assign('customer',$customerAdmin)->display();
	
	}
	public function addAction(){
		$this->provinceOptions = $this->getOption(\Utuo\Region::getProvinceList(), null, '==请选择省份==');
		$this->cityOptions = $this->getOption(array(), null, '==请选择城市==');
		$this->areaOptions = $this->getOption(array(), null, '==请选择地区==');
		$this->assign("fid",$this->cid);
		$this->display();
	}
	public function addPostAction(){
		$fid=I('fid',0,'intval');
		$menus=M("CustomerMenuManager")->where("cid=%d",$fid)->field("id,menu_id")->select();
        $roles=M('RoleCustomer')->where("customer_id =%d",$fid)->select();
        $menuArray=array();
        $roleArray=array();
        $m=D('Customer');
		if($m->create())
		{   $m->partner=$this->aid;
		    $cid=$m->add();
			foreach ($menus as $v)
			{
				$menuArray[]=array(
						'cid'=>$cid,
						'menu_id'=>$v['menu_id'],
				);
			}
			foreach ($roles as $v)
			{
				$roleArray[]=array(
					'customer_id'=>$cid,
						'role_id'=>$v['role_id']
				);
			}
		($cid and M('RoleCustomer')->addAll($roleArray) and M('CustomerMenuManager')->addAll($menuArray))?$this->ajaxSuccess():$this->ajaxError();
		}else {
			$this->ajaxError($m->getError());
		}
	}
	public function modifyAction(){
	     $customer = $this->getOneById("customer");
		if ($customer) {
			$customer['provinceOptions'] = $this->getOption(\Utuo\Region::getProvinceList(), $customer['province'], '==请选择省份==');
			$customer['cityOptions'] = $this->getOption(\Utuo\Region::getCityListByProvince($customer['province']), $customer['city'], '==请选择城市==');
			$customer['areaOptions'] = $this->getOption(\Utuo\Region::getAreaListByCity($customer['city']), $customer['area'], '==请选择地区==');
			$this->assign("customer", $customer);
			$this->display();
		} else {
			$this->error('客户不存在');
		}
	}
	public function modifyPostAction($id){
	$d = D("Customer");
		if ($d->create()) {
			
			$d->partner = $this->aid;
			if (empty($d->password)) {
				unset($d->password);
			} else {
				$d->password =\Utuo\Phpass::hash($d->password);
			}
			if (false !== $d->save()) {
				$this->ajaxSuccess();
			} else {
				$this->ajaxError();
			}
		} else {
			$this->ajaxError($d->getError());
		}
	}
	
	// 删除数据
	public function removePostAction() {
		$this->ajaxRemove('Customer');
	}
	
	// 彻底删除
	public function deletePostAction() {
		$this->ajaxDelete('Customer');
	}
	
	// 恢复管理员
	public function restorePostAction() {
		$this->ajaxRestore('Customer');
	}
	
	public function setStatusPostAction() {
		$this->ajaxSetStatus('CustomerAdmin');
	}
	
}